<?php

namespace app\index\controller;

use think\admin\Controller;
use think\facade\Db;
use think\facade\Request;
use think\facade\Cookie;
/**
 * 员工移动端
 * Class Login
 * @package app\index\controller
 */
class Login extends Controller
{
    /**
     * 员工登录主页
     */
    public function index()
    {

        $oid = Request::param('openid');
        $mob = Request::param('mobile');

        if($oid){
            //Session::set('openid', $oid);
            Cookie::forever('openid', $oid);//openid永久保存
            $openid = $oid;
        }else{
            $openid = Cookie::get('openid');//读取openid
        }

        if($mob){
            //Session::set('openid', $oid);
            Cookie::forever('mobile', $mob);//openid永久保存
            $mobile = $mob;
        }else{
            $mobile = Cookie::get('mobile');//读取openid
        }
        $meal = Db::connect('zs')->table('meal')
            ->where('mobile', $mobile)
            ->whereOr('ago_mobile','like','%'.$mobile.'%')
            ->where('dtype', 0)
            ->find();
        //图标权限
        $mealicon = Db::name('icon_details')
            ->where('status',1)
            ->where('is_deleted',0)
            ->column('onebtype','id');
        $icons = array();
        foreach ($mealicon as $key=>$vo)
        {
            $icon = explode(',',$vo);
            if (in_array($meal['id'],$icon))
            {
                $icons[] = $key;
            }
        }

        //图标展示
        $showid = Db::name('icon_details')
            ->whereIn('id',$icons)
            ->where('status',1)
            ->where('is_deleted',0)
            ->column('onebmeal,title,iconUrl,iconImg');
        //轮播图
        $carousel = Db::name('icon_carousel')
            ->where('status',1)
            ->where('is_deleted',0)
            ->select()->toArray();

        $this->assign('lastname', $meal['lastname']);
        $this->assign('carousel', $carousel);
        $this->assign('uid', $meal['id']);
        $this->assign('icon', $showid);
        $this->assign('name', $meal['lastname']);
        $this->_form(Db::connect('zs')->table('meal'), 'index');
    }
    /**
     * 员工登录
     */
    public function login()
    {
        //员工登录主页
        $this->_form(Db::connect('zs')->table('meal'), 'login');
    }
    /**
     * 员工停车放行
     */
    public function car()
    {
        $oid = Request::param('openid');
        $mob = Request::param('mobile');
        if($mob){
            //Session::set('openid', $oid);
            Cookie::forever('mobile', $mob);//openid永久保存
            $mobile = $mob;
        }else{
            $mobile = Cookie::get('mobile');//读取openid
        }

        $meal = Db::connect('zs')->table('meal')
            ->where('mobile', $mobile)
            ->find();
        //查询员工时间
        $uname = Db::connect('mysql')->table('car_tk_b')
            ->where('uname', $meal['lastname'])
            ->find();
        //员工可用时间
        $freeTime = $uname['freeTime']/24;
        $bid = $uname['bid']?:0;
        $this->assign('bid', $bid);
        $this->assign('freeTime', $freeTime);
        $this->assign('name', $meal['lastname']);
        //员工登录主页
        $this->_form(Db::connect('zs')->table('meal'), 'car');
    }
    /**
     * 停车新接口测试
     */
    public function cars()
    {
        $this->_form(Db::connect('zs')->table('meal'), 'cars');
    }

    /**
     * 应用主页
     */
    public function application()
    {
        $openid = Cookie::get('openid');
        $mobile = Cookie::get('mobile');

        $meal = Db::connect('zs')->table('meal')
            ->where('mobile', $mobile)
            ->find();

        //图标权限
        $mealicon = Db::name('icon_details')
            ->where('status',1)
            ->where('is_deleted',0)
            ->column('onebtype','id');
        $icons = array();
        foreach ($mealicon as $key=>$vo)
        {
            $icon = explode(',',$vo);
            if (in_array($meal['id'],$icon))
            {
                $icons[] = $key;
            }
        }
        //图标展示
        $showid = Db::name('icon_details')
            ->whereIn('id',$icons)
            ->where('status',1)
            ->where('is_deleted',0)
            ->column('onebmeal,title,iconUrl,iconImg,id');

        foreach ($showid as $k => $v)
        {
            $shows = explode(',',$v['onebmeal']);
            if (in_array($meal['id'],$shows))
            {
                $showid[$k]['show'] = 'checked';
            }else{
                $showid[$k]['show'] = '';
            }
        }
        $this->assign('openid', $openid);
        $this->assign('uid', $meal['id']);
        $this->assign('name', $meal['lastname']);
        $this->assign('icon', $showid);
        //var_dump($openid);
        $this->_form(Db::connect('zs')->table('meal'), 'application');
    }


}